package com.inno.log.mq;

import cn.hutool.core.util.IdUtil;
import com.inno.log.entity.OperationLog;
import com.inno.log.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * @author shenzeyang
 */
@Service
@Slf4j
@RocketMQMessageListener(topic = "save-log", consumerGroup = "zey-inno-log")
public class SaveLogListener implements RocketMQListener<OperationLog> {
    private final OperationLogService operationLogService;
    public SaveLogListener(OperationLogService operationLogService){
        this.operationLogService = operationLogService;
    }
    @Override
    public void onMessage(OperationLog operationLog) {
        try {
            operationLog.setId(IdUtil.getSnowflakeNextId());
            operationLogService.insert(operationLog);
        }catch (Exception e){
            log.error("SaveLogListener.onMessage.error : {}" , e.getMessage());
        }
    }
}
